/**
 * @namespace
 */
aws.modules.observer = {
	init: function(context) {
		this.context = context;
		this.refreshSubscribers();
	},
	/**
	 * Refreshes list of subscribed functions
	 */
	refreshSubscribers: function() {
		this.subscribers = {};
		$.each(this.context.modules, $.proxy(function(k, module) {
			if (typeof module.listSubscribers === 'function') {
				var moduleSubscribers = module.listSubscribers();
				$.each(moduleSubscribers, $.proxy(function(event, newSubscribers) {
					this.subscribers[event] = (this.subscribers[event] || []).concat(newSubscribers);
				}, this) );
			}
		}, this) );
	},
	/**
	 * 
	 * @param {Object} eventName Name of event to notify subscribers of
	 * @param {Array} [args] Arguments to be passed to subscribed function
	 * @param {Object} [scope] Context in which subscribed function should be executed
	 */
	notify: function(eventName, args, context) {
		context = context || this.context || window;
		if (this.subscribers[eventName]) {
			$.each(this.subscribers[eventName], function(k, v) {
				if (v.context) {
					context = v.context;
					v = v.exec;
				}
				v.apply(context, args || []);
			});
		}
	},
	/**
	 * Holds list of all events and subscribed functions
	 */
	subscribers: {}
};

var notify = function() {
	aws.modules.observer.notify.apply(aws.modules.observer, arguments)
};
